package com.hongtao3.hsync.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.hongtao3.hsync.Hsync;
import com.hongtao3.hsync.vo.Meta;
import com.hongtao3.hsync.vo.RemoteFile;
import com.hongtao3.hsync.vo.Server;

public class ServerConfigPanel extends JPanel {
private static final long serialVersionUID = 1L;
public ServerConfigPanel(Component owner,Server server){
	   this.owner=owner;
	   this.server=server;
	   ownerDialog=(ServerConfigDialog)owner;	
	   init();
   }
   
   private void init(){
	     setLayout(new FlowLayout());
	     Box rootBox=Box.createVerticalBox();
	     
	     Box baseBox=Box.createHorizontalBox();
		  baseBox.add(createLabelPanel());
		  baseBox.add(Box.createHorizontalStrut(5));
		  baseBox.add(createFieldPanel());
		  
		  initFooterPanel();
		  rootBox.add(new JLabel("  "));
		  rootBox.add(baseBox);
		  rootBox.add(new JLabel("  "));
		  rootBox.add(footerPanel);
		  add(rootBox,BorderLayout.CENTER);
		  
		  aliasField.setText(server.getAlias());
		  hostField.setText(server.getHost_url());
		  localRootField.setText(server.getLocalDocRoot());
		  remoteRootField.setText(server.getRemote_doc_root());
		  userNameField.setText(server.getUserName());
		  passwordField.setText(server.getPassword());
   }
   
   private void initFooterPanel(){
	   footerPanel=new JPanel();
	   footerPanel.setLayout(new FlowLayout());
	   
	   initConnectBt();
	   saveButton=new JButton("save");
	   footerPanel.add(saveButton);
	   
	   JButton closeBt=new JButton("close");
	   footerPanel.add(closeBt);
	   closeBt.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			owner.setVisible(false);
		}
	   });
	   
	   JButton newBt=new JButton("new");
	   footerPanel.add(newBt);
	   newBt.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Server ser=new Server();
			ser.setAlias("new");
			ownerDialog.addNewTable(ser);
		}
	});
   }
   
   

   
   private void initConnectBt(){
 	  connectButton=new JButton("connect");
 	  footerPanel.add(connectButton);
 	  connectButton.addActionListener(new ActionListener() {
 		@Override
 		public void actionPerformed(ActionEvent arg0) {
 			 hsync=new Hsync(getHostUrl(), getRemoteRoot(), getUserName(), getPassword());
 			try {
 				HashMap<String, Meta> ms=hsync.getRootMeta();
// 				JOptionPane.showMessageDialog(null,"connect success!");
 				 RemoteFile.hsync=hsync;
 				 RemotePanel rp=MainFrame.mainFrame.getRemotePanel();
 				 rp.showPath(getRemoteRoot(),true);
 				 MainFrame.mainFrame.setTitle(server.getAlias());
 				 ownerDialog.setVisible(false);
 				 LocalPanel lp=MainFrame.mainFrame.getLocalPanel();
 				 lp.showPath(getLocalRoot(),true);
 			} catch (Exception e) {
 				e.printStackTrace();
 				JOptionPane.showMessageDialog(null, e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
 			}
 		}
 	});
   }
   
   private Box createLabelPanel(){
 	  Box b=Box.createVerticalBox();
 	  b.add(new Label("alias:"));
 	  b.add(new Label("local root:"));
 	  b.add(new Label("host  url:"));
 	  b.add(new Label("remote root:"));
 	  b.add(new Label("userName:"));
 	  b.add(new Label("password:"));
 	  return b;
   }
   
   private Box createFieldPanel(){
 	  Box b=Box.createVerticalBox();
 	  aliasField=new JTextField(20);
 	  b.add(aliasField);
 	  localRootField=new JTextField(20);
 	  b.add(localRootField);
 	  hostField=new JTextField(20);
 	  b.add(hostField);
 	  remoteRootField=new JTextField(20);
 	  b.add(remoteRootField);
 	  userNameField=new JTextField(20);
 	  b.add(userNameField);
 	  passwordField=new JPasswordField(20);
 	  b.add(passwordField);
 	  return b;
   }
   
   private String getAlias(){
 	  return aliasField.getText();
   }
   private String getHostUrl(){
 	  return hostField.getText();
   }
   
   private String getRemoteRoot(){
 	  return  remoteRootField.getText();
   }
   private String getLocalRoot(){
	   return  localRootField.getText();
   }
   private String  getUserName(){
 	  return userNameField.getText();
   }
   
   private String getPassword(){
 	 return  new String(passwordField.getPassword());
   }
   
   private JTextField aliasField;
   private JTextField hostField;
   private JTextField remoteRootField;
   private JTextField localRootField;
   private JTextField userNameField;
   private JPasswordField passwordField;
   
   private JPanel footerPanel;
   private JButton saveButton;
   private JButton connectButton;
   
   private Component owner;
   private ServerConfigDialog ownerDialog;
   private Server server;
   private Hsync hsync;
}
